package com.gsq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gsq.common.pojo.Admin;
import com.gsq.common.vo.PageResult;
import com.gsq.mapper.AdminMapper;
import com.gsq.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.UUID;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    /* 登陆 */
    @Override
    public String login(Admin admin) {
        //  判断用户状态 0为停用 1为启用
        //1.获取原始密码
            String password = admin.getPassword();
        //2.将密码进行加密处理
        String md5Str = DigestUtils.md5DigestAsHex(password.getBytes());
        admin.setPassword(md5Str);
        //3. 截取密文部分长度
        String partMd5Str = md5Str.substring(5,16);
        //4. 获取随机盐
        String salt = UUID.randomUUID().toString().replace("-", "");
        //5. 拼接新密码
        String newPassword = DigestUtils.md5DigestAsHex((md5Str + partMd5Str + salt).getBytes());
        //6.将密文传递给对象
        if (admin.getSalt()==null)
            admin.setSalt(newPassword); //username/password
        QueryWrapper<Admin> queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", admin.getUsername());
        //7.根据条件查询数据库
        Admin adminDB = adminMapper.selectOne(queryWrapper);
        //8.定义token数据 限定条件 token不能重复
        // UUID: 随机字符串 3.4*10^38 种可能性
        String uuid = UUID.randomUUID().toString()
                .replace("-","");
        return adminDB==null?null:uuid;
    }

    /**
     * 管理用户数据展现
     * @param pageResult
     * @return
     */
    @Override
    public PageResult getAdminList(PageResult pageResult) {
        IPage iPage = new Page(pageResult.getPageNum(),pageResult.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(flag, "username",pageResult.getQuery());
        iPage = adminMapper.selectPage(iPage, queryWrapper);
        long total = iPage.getTotal();
        List<Admin> rows = iPage.getRecords();
        return pageResult.setTotal(total).setRows(rows);
    }
    /* 状态修改 */
    @Override
    @Transactional
    public void updatedValid(Admin admin) {
        adminMapper.updateById(admin);
    }
    /* 增添用户 */
    @Override
    public void addAdmin(Admin admin) {
        //1. 获取添加时的密码
        String password = admin.getPassword();
        //2. 对密码进行加密
        String md5pwd = DigestUtils.md5DigestAsHex(password.getBytes());
        //3. 获取盐值
        String salt = UUID.randomUUID().toString().replace("-", " ");
        //4. 截取部分md5
        String partMd5 = md5pwd.substring(5,16);
        //5. 拼接新密码
        String newPassword = DigestUtils.md5DigestAsHex((md5pwd+partMd5+salt).getBytes());
        if (admin.getSalt()==null)
            admin.setSalt(newPassword);
        admin.setValid(true).setPassword(md5pwd);
        adminMapper.insert(admin);
    }
    /* 修改用户信息 */
    @Override
    public void updateAdmin(Admin admin) {
        adminMapper.updateById(admin);
    }
    /* 删除用户 */
    @Override
    public void delelteAdmin(Integer id) {
        adminMapper.deleteById(id);
    }
    /* 根据id反馈信息*/
    @Override
    public Admin retrunAdmin(Integer id) {
        return adminMapper.selectById(id);
    }

}
